package cc.yugu.yhdw.common.protocol.msg05;

import cc.yugu.yhdw.common.protocol.MsgCode;
import cc.yugu.yhdw.common.tool.netty.TypeOperator;
import io.netty.buffer.ByteBuf;

import java.util.ArrayList;
import java.util.List;

public class Msg051000012 extends Msg051 {

    public String cabinetId;
    public List<Customer> createList = new ArrayList<Customer>();
    public List<Customer> updateList = new ArrayList<Customer>();
    public List<Customer> deleteList = new ArrayList<Customer>();

    @Override
    public int getMsgCode() {
        return MsgCode.MSG_051000012.getCode();
    }


    @Override
    public void writeData(ByteBuf buffer) {
        writeString(buffer, cabinetId);
        buffer.writeInt(createList.size());
        for (Customer e : createList) {
            e.write(buffer);
        }
        buffer.writeInt(updateList.size());
        for (Customer e : updateList) {
            e.write(buffer);
        }
        buffer.writeInt(deleteList.size());
        for (Customer e : deleteList) {
            e.write(buffer);
        }
    }

    @Override
    public void readData(ByteBuf buffer) {
        cabinetId = readString(buffer);
        int size1 = buffer.readInt();
        for (int i = 0; i < size1; i++) {
            Customer customer = new Customer();
            customer.read(buffer);
            createList.add(customer);
        }
        int size2 = buffer.readInt();
        for (int i = 0; i < size2; i++) {
            Customer customer = new Customer();
            customer.read(buffer);
            updateList.add(customer);
        }
        int size3 = buffer.readInt();
        for (int i = 0; i < size3; i++) {
            Customer customer = new Customer();
            customer.read(buffer);
            deleteList.add(customer);
        }
    }

    public static class Customer extends TypeOperator {
        public int id;
        public String mobile;
        public String password;
        public int balance;
        public String batteryId;


        public void write(ByteBuf buffer) {
            buffer.writeInt(id);
            writeString(buffer, mobile);
            writeString(buffer, password);
            buffer.writeInt(balance);
            writeString(buffer, batteryId);

        }

        public void read(ByteBuf buffer) {
            id = buffer.readInt();
            mobile = readString(buffer);
            password = readString(buffer);
            balance = buffer.readInt();
            batteryId = readString(buffer);
        }
    }
}
